<? 
	include('base.php');
	User::protect();
	include_class('shows');
	include_class('tours');
	include_class('venues');
	include_class('locations');
	include_class('band_members');
	$sh = Show::get($_GET['id']);
	
	if (!db::isError($sh)) {
		switch($_GET['task']) {
			case 'update':
				$res = $sh->update($_POST);
				if (!db::isError($res)) {
					header('Location: show_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'deactivate':
				$res = $sh->deactivate();
				if (!db::isError($res)) {
					header('Location: show_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'activate':
				$res = $sh->activate();
				if (!db::isError($res)) {
					header('Location: show_edit.php?id=' . $_GET['id']);
				}
				break;
			case 'delete':
				$res = $sh->remove();
				if (!db::isError($res)) {
					header('Location: shows.php');
				}
				break;
		}
	}
	$editors = array('notes');
	$section = 'shows';
	$calendar = true;
	$javascript = 'venues.js';
	
	$page_title = 'Edit Show';
	include('layout/header.php');
?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="live.php">Live &#62;</a>
	<a href="shows.php">Shows &#62;</a>
	View/Edit Show
</div>

<? 	if (!db::isError($sh)) { 
	if (db::isError($res)) {
		$res->outputList();
	}
	
	if (($_GET['task'] == 'edit' || $_GET['task'] == 'update') && $sh->canEdit()) { ?>
		
		<h1>edit show:</h1>
		
		<div class="inset">
		<form id="edit_entry" action="<?=$PHP_SELF?>?task=update&amp;id=<?=$_GET['id']?>" method="post">
		<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
		<? if (!$sh->isActive()) { ?>
		<tr>	
			<td colspan="4" class="fieldname">This show is inactive.</td>
		</tr>
		<? } ?>
		
		<?
			
			$ve = $sh->getVenueObject();
			
			$sl = new VenueList();
			$limiter = array();
			
			if (is_object($ve) && !db::isError($ve)) {
				$country = ($_REQUEST['country']) ? $_REQUEST['country'] : $ve->getCountry();
				$state = ($_REQUEST['stateProvince']) ? $_REQUEST['stateProvince'] : $ve->getStateProvince();
				$city = ($_REQUEST['city']) ? $_REQUEST['city'] : $ve->getCity();
				$venueID = $ve->getID();
			} else {
				$country = $_REQUEST['country'];
				$state = $_REQUEST['stateProvince'];
				$city = $_REQUEST['city'];
				$venueID = 0;
			}
			
			$limiter['country'] = $country;			
			$limiter['stateProvince'] = $state;
			$limiter['city'] = $city;
			
			$ctl = new CountryList();
			$countryList = $ctl->getCountries();
			$cotl = $sl->getCountryList();
			$stl = $sl->getStateProvinceList($country);
			$ctl = $sl->getCityList($limiter);
			$venuelist = $sl->get($limiter);
			
			$name = $_POST ? $_POST['name'] : $sh->getName();
			$date = $_POST ? $_POST['date'] : $sh->getDate();
			$time = $_POST ? $_POST['time'] : $sh->getTimeValue();
			$cost = $_POST ? $_POST['cost'] : $sh->getCostValue();
			$user_id = $_POST ? $_POST['user_id'] : $sh->getUserID();
			$isAllAges = $_POST ? $_POST['is_all_ages'] : $sh->isAllAges();
			$other_bands = $_POST ? $_POST['other_bands'] : $sh->getOtherBands();
			$notes = $_POST ? $_POST['notes'] : $sh->getNotes();
			
		?>
	
	
		<tr>
			<td width="50%"><strong>Country</strong><br/>
			<select name="country" id="countries">
				<option value="">** Select Country</option>
			<? foreach ($cotl as $ct => $total) { ?>
				<option value="<?=$ct?>"<? if ($country == $ct) { ?> selected<? } ?>><?=$countryList[$ct]?> (<?=$total?>)</option>
			<? } ?>
			</select>
			</td>
			<td width="50%"><strong>State/Province</strong><br/>
			<select name="stateProvince" id="stateProvince">
				<option value="">** Select State/Province</option>
			<? foreach ($stl as $sp => $total) {
				$item = ($stateList[$sp] == null) ? $sp : $stateList[$sp]; ?>
				<option value="<?=$sp?>"<? if ($state == $sp) { ?> selected<? } ?>><?=$item?> (<?=$total?>)</option>
			<? } ?>
			</select>
			</td>
		</tr>
			<td width="50%"><strong>City</strong><br/>
			<select name="city" id="city">
				<option value="">** Select City</option>
			<? foreach ($ctl as $ci => $total) { ?>
				<option value="<?=$ci?>"<? if ($city == $ci) { ?> selected<? } ?>><?=$ci?> (<?=$total?>)</option>
			<? } ?>
			</select>
			</td>
			<td width="50%"><strong>Venue</strong> (<a href="venue_add.php" target="_blank">Add Venue</a>)<br/>
			<select name="venue_id" id="venue">				
			<? foreach ($venuelist as $vn) { ?>
				<option value="<?=$vn->getID()?>"<? if ($vn->getID() == $venueID) { ?> selected<? } ?>><?=$vn->getName()?></option>
			<? } ?>
				<option value="0"<? if ($venueID == 0) { ?> selected<? } ?>>** No Venue</option>
			</select>
			</td>
		</tr>
		</table>
		<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
		<tr>
			<td width="33%"><strong>Name</strong><br/><input type="text" name="name" value="<?=$name?>" class="text"></td>
			<td width="34%"><strong>Date (<a style="vertical-align: middle" href="javascript:void(0)" onclick="popUpCalendar(this, document.getElementById('date'), '<?=DATE_JS_CALENDAR?>')">Calendar</a>)</strong><br/>
			<input name="date" id="date" class="text" type="text" value="<?=$date?>">			
			</td>
			<td width="33%"><strong>Time</strong><br/><input type="text" name="time" value="<?=$time?>"></td>
		</tr>
		<tr>
			<td><strong>Cost</strong><br/><input type="text" name="cost" value="<?=$cost?>" class="text" ></td>
			
			<td><strong>Poster</strong> (* - inactive)<br/>
			<? if (User::isAdmin()) { ?>
					<select name="user_id">
						<?
						$ul = new UserList();
						$uArray = $ul->getAdminUsers();
						foreach ($uArray as $uo) { ?>
							<option class="admin-user" value="<?=$uo->getID()?>"<? if ($user_id == $uo->getID()) { ?> SELECTED<? } ?>><?=$uo->getFirstName()?><? if (!$uo->isActive()) { ?> *<? } ?></option>
						<? }
						
						$bmlist = new BandMemberList();
						$bmarray = $bmlist->get_band_members();
						foreach ($bmarray as $bm) {
							$uo = $bm->getUserObject();
						?>
							<option value="<?=$uo->getID()?>"<? if ($user_id == $uo->getID()) { ?> SELECTED<? } ?>><?=$uo->getFirstName()?><? if (!$uo->isActive()) { ?> *<? } ?></option>
						<? } ?>
					</select>
		
			<? } else { 
				$uo = $sh->getUserObject();
				echo($uo->getFirstName());
			} ?>					
			</td>
			<td><strong>Is All Ages?</strong><br/><input type="radio" name="is_all_ages" value="1"<? if ($isAllAges) { ?> checked<? } ?>> Yes
				<input type="radio" name="is_all_ages" value="0"<? if (!$isAllAges) { ?> checked<? } ?>> No				
			</td>
		</tr>
		</table>
		<div class="edit-form">
			<strong>Other Bands</strong><br/><input type="text" class="text" name="other_bands" value="<?=$other_bands?>">
		</div>
		<div class="edit-form">
			<strong>Notes</strong><br/><textarea name="notes" id="notes" class="full"><?=$notes?></textarea>
		</div>
		<div class="edit-form">
			<input type="submit" value="Update Show" class="button" name="submit">
			<input type="submit" value="Exit Without Updating" class="button" name="preview" onclick="location.href='<?=$PHP_SELF?>?id=<?=$sh->getID()?>'; return false">
		</div>
		</div>
		
	<? } else { ?>

		<h1>view show:</h1>
		<div class="inset">
		
		<h2><?
			echo($sh->getDate());
			echo(': ' . $sh->getName());
		?></h2>
		
		<span class="small">
			Time: <strong><?= $sh->getTime() ?></strong> |
			Cost: <strong><?= $sh->getCost() ?></strong> |
			All Ages: <strong><? if ($sh->isAllAges()) { ?>Yes<? } else { ?>No<? } ?></strong> |
			Status: <strong><? if ($sh->isActive()) { ?>Active<? } else { ?>Inactive<? } ?></strong> |
			<? $uo = $sh->getUserObject() ?>
			Poster: <strong><?= $uo->getFirstName() ?></strong>
		</span>
		
		<? 
			$tour = $sh->getTourObject();
			if (!db::isError($tour)) { ?>
				<br><br>
				<strong>Tour</strong><br>
				<a href="tour_edit.php?id=<?=$tour->getID()?>"><?=$tour->getTitle()?></a>
			<? } ?>
			
		<br><br>
		<strong>Playing At</strong><br>
		<? $ve = $sh->getVenueObject(); ?>
		<? if (is_object($ve) && !db::isError($ve)) { ?>
			<?=$ve->getName()?><br/>
			<?=$ve->getAddress1()?><br/>
			<? 
			if ($ve->getAddress2) {
				echo($ve->getAddress2() . '<br/>');
			}
			if ($ve->getCity()) {
				echo($ve->getCity());
			} else {
				echo("(no city)");
			}
			echo(", ");
			if ($ve->getStateProvince()) {
				echo($ve->getStateProvince());
			} else {
				echo("(no state/province)");
			}
			echo("&nbsp;&nbsp;");
			if ($ve->getPostalCode()) {
				echo($ve->getPostalCode());
			} else {
				echo("(no zip/postal)");
			}
			echo("<br/>");
			echo($ve->getCountryName());
			
			echo("<br/><br/>");
		} else { ?>
			No venue.<br/><br/>
		<? }
		
		if ($sh->getOtherBands()) { ?>
			<b>Other Bands Playing:</b><br>
			<?=$sh->getOtherBands()?><br><br>
		<? } ?>
		<? if ($sh->getNotes()) { ?>
			<b>Miscellaneous Notes:</b><br>
			<?=$sh->getNotes()?><br><br>
		<? } ?>
		<form name="entry_status" method="get">
			<? if ($sh->canEdit()) { ?>
				<input type="submit" name="" value="Edit Show" onclick="location.href='<?=$PHP_SELF?>?task=edit&id=<?=$_GET['id']?>';return false" class="button">
			<? } ?>
			<input type="button" name="" value="Photos" onclick="location.href='show_media.php?id=<?=$_GET['id']?>&type=photos';return false" class="button">
			<input type="button" name="" value="Audio/Video" onclick="location.href='show_media.php?id=<?=$_GET['id']?>&type=av';return false" class="button">
			<? if ($sh->canEdit()) { ?>
				<? if ($sh->isActive()) { ?><input type="submit" name="" value="Deactivate" onclick="location.href='<?=$PHP_SELF?>?task=deactivate&id=<?=$_GET['id']?>';return false" class="button"><? } else { ?><input type="submit" name="" value="Activate" onclick="location.href='<?=$PHP_SELF?>?task=activate&id=<?=$_GET['id']?>';return false" class="button"><? } ?>
			<? } ?>
			<? if (User::isAdmin()) { ?>			
				<input type="submit" value="Remove" name="" onclick="if (confirm('Are you sure you wish to delete this show?\rWhenever possible, deactivate rather than delete.')) { location.href='<?=$PHP_SELF?>?task=delete&id=<?=$_GET['id']?>'};return false" class="button">
			<? } ?>
			<input type="submit" value="Return to Shows" name="" onclick="location.href='shows.php'; return false" class="button">
		</form>
		
		</div>
	<? } ?>

<? } else { 
	$sh->outputDialog('Return to Shows','shows.php');
} ?>

<? include('layout/footer.php'); ?>
